package adventure.commands.skills;

import adventure.Adventure;
import adventure.placeable.ItemImp;
import java.util.ArrayList;
import player.PlayerManager;

/**
 *
 * @author Michael Hanns
 *
 */
public class SkillManagerImp implements SkillManager {

    private ArrayList<Skill> skills;
    private Adventure adv;

    public SkillManagerImp(Adventure a) {
        skills = new ArrayList<Skill>();
        adv = a;
        skills.add(new SingleTargetDamage(a, 0));
    }

    @Override
    public void newSkill(String type) {
        if (type.equalsIgnoreCase(SingleTargetDamage.type)) {
            skills.add(new SingleTargetDamage(adv, new String[]{""}, skills.size()));
        } else if (type.equalsIgnoreCase(SingleTargetHeal.type)) {
            skills.add(new SingleTargetHeal(adv, new String[]{""}, skills.size()));
        } else if (type.equalsIgnoreCase(ConjureItem.type)) {
            skills.add(new ConjureItem(adv, new String[]{""}, skills.size(), new ItemImp()));
        } else {
            // Trashy "all else fails, throw something standard" option
            skills.add(new SingleTargetDamage(adv, new String[]{""}, skills.size()));
        }
    }

    @Override
    public void newSkill(SkillEdit s) {
        skills.add((Skill) s);
    }

    @Override
    public int totalSkills() {
        return skills.size() - 1;
    }

    @Override
    public Skill getSkill(int id) {
        for (int c = 0; c < skills.size(); c++) {
            if (((SkillEdit) skills.get(c)).getID() == id) {
                return skills.get(c);
            }
        }
        return new GenericSkill(adv);
    }

    @Override
    public String[] getCategories() {
        ArrayList<String> categoriesAR = new ArrayList<String>();
        boolean duplicate = false;
        for (int c = 0; c < skills.size(); c++) {
            for (int d = 0; d < categoriesAR.size() && !duplicate; d++) {
                if (skills.get(c).getCategory().equalsIgnoreCase(categoriesAR.get(d))) {
                    duplicate = true;
                }
            }
            if (!duplicate) {
                categoriesAR.add(skills.get(c).getCategory());
            } else {
                duplicate = false;
            }
        }
        String[] out = new String[categoriesAR.size()];
        System.out.println("Categories: " + out.length);

        return categoriesAR.toArray(out);

    }

    @Override
    public void removeSkill(int id) {
        for (int c = 0; c < skills.size(); c++) {
            if (((SkillEdit) skills.get(c)).getID() == id) {
                skills.remove(c);
            }
        }
    }

    @Override
    public void changeSkillType(int id, String newType) {
        SkillEdit oldSkill = (SkillEdit) getSkill(id);
        if (newType.equalsIgnoreCase(SingleTargetDamage.type)) {
            SkillEdit newSkill = new SingleTargetDamage(adv, oldSkill.getSynonyms(), oldSkill.getID());
            newSkill.setAttackDesc(oldSkill.getAttackDesc());
            newSkill.setActionFlag(oldSkill.getAttackFlag());
            newSkill.setCooldown(oldSkill.getCooldown());
            newSkill.setDescription(oldSkill.getDescription());
            newSkill.setDice(oldSkill.getNumDice(), oldSkill.getDiceSides());
            newSkill.setLevelToLearn(oldSkill.levelLearned());
            newSkill.setMainStat(oldSkill.getMainStat());
            newSkill.setSpecialReq(newSkill.getSpecialReq());

            skills.set(id, (Skill) newSkill);
        } else if (newType.equalsIgnoreCase(SingleTargetHeal.type)) {
            SkillEdit newSkill = new SingleTargetHeal(adv, oldSkill.getSynonyms(), oldSkill.getID());
            newSkill.setAttackDesc(oldSkill.getAttackDesc());
            newSkill.setActionFlag(oldSkill.getAttackFlag());
            newSkill.setCooldown(oldSkill.getCooldown());
            newSkill.setDescription(oldSkill.getDescription());
            newSkill.setDice(oldSkill.getNumDice(), oldSkill.getDiceSides());
            newSkill.setLevelToLearn(oldSkill.levelLearned());
            newSkill.setMainStat(oldSkill.getMainStat());
            newSkill.setSpecialReq(newSkill.getSpecialReq());

            skills.set(id, (Skill) newSkill);
        } else if (newType.equalsIgnoreCase(ConjureItem.type)) {
            SkillEdit newSkill = new ConjureItem(adv, oldSkill.getSynonyms(), oldSkill.getID(), new ItemImp());
            newSkill.setAttackDesc(oldSkill.getAttackDesc());
            newSkill.setActionFlag(oldSkill.getAttackFlag());
            newSkill.setCooldown(oldSkill.getCooldown());
            newSkill.setDescription(oldSkill.getDescription());
            newSkill.setDice(oldSkill.getNumDice(), oldSkill.getDiceSides());
            newSkill.setLevelToLearn(oldSkill.levelLearned());
            newSkill.setMainStat(oldSkill.getMainStat());
            newSkill.setSpecialReq(newSkill.getSpecialReq());

            skills.set(id, (Skill) newSkill);
        }
    }

    @Override
    public void setPlayerManager(PlayerManager players) {
        for (int c = 0; c < skills.size(); c++) {
            skills.get(c).setPlayerManager(players);
        }
    }
}
